//
//  VistorLoginView.m
//  微博
//
//  Created by 沈艳男 on 15/10/29.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

#import "VistorLoginView.h"
#import "DiscoverTableViewController.h"
#import "Masonry.h"

@interface VistorLoginView ()
@property (nonatomic, strong) UIImageView *smalliconView;
@property (nonatomic, strong) UIImageView *maskIconView;
@property (nonatomic, strong) UIImageView *houseImageView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *loginButton;

@end

@implementation VistorLoginView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
}

- (void)leftBtnClick
{
    [self.delegate btnClickRegister];
}

- (void)rightBtnClick
{
    [self.delegate btnClickLogin];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
        [self setUpUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUpUI];
    };
    return self;
}



- (void)setUpInfoImageName:(NSString *)imageName message:(NSString *)message
{
    if (imageName != nil) {
        self.smalliconView.image = [UIImage imageNamed:imageName];
        self.messageLabel.text = message;
        // 隐藏主页的小房子图片
        self.houseImageView.hidden = YES;
        // 把蒙版 “maskIconView” 设置到图片的后面
        [self sendSubviewToBack:self.maskIconView];
    }else {
        [self startAnimation];
    }
    
}

- (void)startAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.toValue = @(2 * M_PI);
    animation.duration = 2;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    [self.smalliconView.layer addAnimation:animation forKey:nil];
}

#pragma mark - 加载控件
- (void)setUpUI
{
    [self addSubview:self.smalliconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.houseImageView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    self.smalliconView.translatesAutoresizingMaskIntoConstraints = NO;
    self.maskIconView.translatesAutoresizingMaskIntoConstraints = NO;
    self.houseImageView.translatesAutoresizingMaskIntoConstraints = NO;
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.registerButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSLayoutConstraint *smalliconViewConstraint1 = [NSLayoutConstraint constraintWithItem:self.smalliconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:(NSLayoutAttributeCenterX) multiplier:1 constant:0];
    NSLayoutConstraint *smalliconViewConstraint2 = [NSLayoutConstraint constraintWithItem:self.smalliconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:(NSLayoutAttributeCenterY) multiplier:1 constant:-60];
    
    NSLayoutConstraint *maskIconViewConstraint1 = [NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.smalliconView attribute:NSLayoutAttributeTop multiplier:1 constant:-400];
    NSLayoutConstraint *maskIconViewConstraint2 = [NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    NSLayoutConstraint *maskIconViewConstraint3 = [NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0];
    NSLayoutConstraint *maskIconViewConstraint4 = [NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-100];
    
    NSLayoutConstraint *houseImageViewConstraint1 = [NSLayoutConstraint constraintWithItem:self.houseImageView attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self.smalliconView attribute:(NSLayoutAttributeCenterX) multiplier:1 constant:0];
    NSLayoutConstraint *houseImageViewConstraint2 = [NSLayoutConstraint constraintWithItem:self.houseImageView attribute:(NSLayoutAttributeCenterY) relatedBy:(NSLayoutRelationEqual) toItem:self.smalliconView attribute:(NSLayoutAttributeCenterY) multiplier:1 constant:0];
    
    NSLayoutConstraint *messageLabelConstraint1 = [NSLayoutConstraint constraintWithItem:self.messageLabel attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self.smalliconView attribute:(NSLayoutAttributeBottom) multiplier:1 constant:0];
    NSLayoutConstraint *messageLabelConstraint2 = [NSLayoutConstraint constraintWithItem:self.messageLabel attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self.smalliconView attribute:(NSLayoutAttributeLeft) multiplier:1 constant:-30];
    NSLayoutConstraint *messageLabelConstraint3 = [NSLayoutConstraint constraintWithItem:self.messageLabel attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:self.smalliconView attribute:(NSLayoutAttributeLeft) multiplier:1 constant:self.smalliconView.bounds.size.width + 30];
    
    NSLayoutConstraint *registerButtonConstraint1 = [NSLayoutConstraint constraintWithItem:self.registerButton attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self.messageLabel attribute:(NSLayoutAttributeBottom) multiplier:1 constant:0];
    NSLayoutConstraint *registerButtonConstraint2 = [NSLayoutConstraint constraintWithItem:self.registerButton attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self.messageLabel attribute:(NSLayoutAttributeLeft) multiplier:1 constant:0];
    NSLayoutConstraint *registerButtonConstraint3 = [NSLayoutConstraint constraintWithItem:self.registerButton attribute:(NSLayoutAttributeWidth) relatedBy:(NSLayoutRelationEqual) toItem:nil attribute:(NSLayoutAttributeNotAnAttribute) multiplier:1 constant:100];
    
    NSLayoutConstraint *loginButtonConstraint1 = [NSLayoutConstraint constraintWithItem:self.loginButton attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self.registerButton attribute:(NSLayoutAttributeTop) multiplier:1 constant:0];
    NSLayoutConstraint *loginButtonConstraint2 = [NSLayoutConstraint constraintWithItem:self.loginButton attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:self.messageLabel attribute:(NSLayoutAttributeRight) multiplier:1 constant:0];
    NSLayoutConstraint *loginButtonConstraint3 = [NSLayoutConstraint constraintWithItem:self.loginButton attribute:(NSLayoutAttributeWidth) relatedBy:(NSLayoutRelationEqual) toItem:nil attribute:(NSLayoutAttributeNotAnAttribute) multiplier:1 constant:100];

    
    [self addConstraint:smalliconViewConstraint1];
    [self addConstraint:smalliconViewConstraint2];
    [self addConstraint:maskIconViewConstraint1];
    [self addConstraint:maskIconViewConstraint2];
    [self addConstraint:maskIconViewConstraint3];
    [self addConstraint:maskIconViewConstraint4];
    [self addConstraint:houseImageViewConstraint1];
    [self addConstraint:houseImageViewConstraint2];
    [self addConstraint:messageLabelConstraint1];
    [self addConstraint:messageLabelConstraint2];
    [self addConstraint:messageLabelConstraint3];
    [self addConstraint:registerButtonConstraint1];
    [self addConstraint:registerButtonConstraint2];
    [self addConstraint:registerButtonConstraint3];
    [self addConstraint:loginButtonConstraint1];
    [self addConstraint:loginButtonConstraint2];
    [self addConstraint:loginButtonConstraint3];

}

#pragma mark - 懒加载控件

- (UIImageView *)smalliconView
{
    if (!_smalliconView) {
        UIImageView *smalliconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
        _smalliconView = smalliconView;
    }
    return _smalliconView;
}

- (UIImageView *)houseImageView
{
    if (!_houseImageView) {
        UIImageView *houseImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
        _houseImageView = houseImageView;
    }
    return _houseImageView;
}

- (UIImageView *)maskIconView
{
    if (!_maskIconView) {
        UIImageView *maskIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"] highlightedImage:nil];
        _maskIconView = maskIconView;
    }
    return _maskIconView;
}

- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        UILabel *messageLabel = [UILabel new];
        messageLabel.numberOfLines = 0;
        messageLabel.font = [UIFont systemFontOfSize:14];
        messageLabel.text = @"关注一些人，回这里看看有什么惊喜，关注一些人，回这里看看有什么惊喜";
        messageLabel.textColor = [UIColor grayColor];
        _messageLabel = messageLabel;
    }
    return _messageLabel;
}

- (UIButton *)registerButton
{
    if (!_registerButton) {
        UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [registerButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:(UIControlStateNormal)];
        [registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [registerButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
        [registerButton addTarget:self action:@selector(leftBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
        _registerButton = registerButton;
    }
    return _registerButton;
}

- (UIButton *)loginButton
{
    if (!_loginButton) {
        UIButton *loginButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [loginButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:(UIControlStateNormal)];
        [loginButton setTitle:@"登录" forState:(UIControlStateNormal)];
        [loginButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
        [loginButton addTarget:self action:@selector(rightBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
        _loginButton = loginButton;
    }
    return _loginButton;
}
@end
